package my.test.csdn.weixin_43767015.basic.ch12.transaction.declarative.xml.claim.service.impl;

import java.io.FileNotFoundException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.transaction.support.DefaultTransactionStatus;

import my.test.csdn.weixin_43767015.basic.ch12.transaction.declarative.xml.claim.entities.TxStudy;
import my.test.csdn.weixin_43767015.basic.ch12.transaction.declarative.xml.claim.service.TxStudyService;

@Service
@Transactional(timeout = 30)
public class TxStudyServiceImpl implements TxStudyService{

    /**
     * jdbc模版，用于操作数据库
     */
    @Resource
    private JdbcTemplate jdbcTemplate;
	
    /*这里仅仅是为了测试方便，省去了DAO/Mapper层*/
    @Transactional(readOnly = true, timeout = 30)
	@Override
	public TxStudy getTxStudy(Long id) {
	    DefaultTransactionStatus transactionStatus = (DefaultTransactionStatus) TransactionAspectSupport.currentTransactionStatus();
	    System.out.println("getTxStudy method Transaction isReadOnly: " + transactionStatus.isReadOnly());
        String sql = "select * from tx_study where id = ?";
        return jdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(TxStudy.class), id);
	}

    @Transactional(readOnly = true, timeout = 30)
	@Override
	public List<TxStudy> getTxStudy(String name) {
		String sql = "select * from tx_study where name like ?";
        return jdbcTemplate.query(sql, BeanPropertyRowMapper.newInstance(TxStudy.class), "%" + name + "%");
	}

    /** 使用标注在类上的@Transactional注解*/
	@Override
	public void insertTxStudy(TxStudy txStudy) throws FileNotFoundException {
	    DefaultTransactionStatus transactionStatus = (DefaultTransactionStatus) TransactionAspectSupport.currentTransactionStatus();
	    System.out.println("insertTxStudy method Transaction isReadOnly: " + transactionStatus.isReadOnly());	
        String sql = "insert into tx_study (name,age) values (?,?)";
        jdbcTemplate.update(sql, txStudy.getName(), txStudy.getAge());
        //抛出一个RuntimeException
        //throw new RuntimeException();
        //抛出一个FileNotFoundException
        throw new FileNotFoundException();
	}

	/** 使用标注在类上的@Transactional注解*/
	@Override
	public void updateTxStudy(TxStudy txStudy) {
        String sql = "UPDATE tx_study SET name=?, age=? where id = ?";
        jdbcTemplate.update(sql, txStudy.getName(), txStudy.getAge(), txStudy.getId());		
	}

	/** 使用标注在类上的@Transactional注解*/
	@Override
	public void test(TxStudy txStudy) {
		// TODO Auto-generated method stub
		try {
			//业务代码
			String sql = "insert into tx_study (name,age) values (?,?)";
			jdbcTemplate.update(sql, txStudy.getName(), txStudy.getAge());
			int i = 1/0;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//捕获异常之后，以编程方式手动触发回滚
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
	}
	
}
